'use strict';

const utils = require('utility');

const Controller = require('egg').Controller;

class HomeController extends Controller {

  async usercreat() {
    const { ctx } = this;
    const { username, sex, birthday, role, area, phone, pas, signature } = ctx.request.body;
    if (!(phone && pas)) {
      return ctx.body = { success: false, msg: '请填写必要的信息' };
    }
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
      ctx.body = { success: false, msg: '请输入正确的手机号' };
      return;
    }
    try {
      const res = await ctx.model.User.findAll({
        where: {
          phone,
        },
      });
      if (res[0]) {
        ctx.body = { success: false, msg: '该手机已注册' };
        return;
      }
      const password = utils.md5(pas);
      try {
        await ctx.model.User.create({
          username,
          sex,
          birthday,
          role: role ? role : '普通用户',
          area,
          phone,
          pas: password,
          addTime: Date.now(),
          signature: signature ? signature : '快来设置您的个性签名吧',
          uportrait: 'https://icweiliimg6.pstatp.com/weili/sm/254329013950218271.jpg',
        });
        ctx.body = { success: true, msg: '创建成功' };
      } catch (e) {
        ctx.body = { success: false, msg: '创建失败' };
      }
    } catch (e) {
      ctx.body = { success: false, msg: '创建失败' };
    }
  }

  async userupdate() {
    const { ctx } = this;
    const { uid, username, sex, birthday, role, area, phone, pas, signature, uportrait } = ctx.request.body;
    if (!uid) {
      return ctx.body = { success: false, msg: '请传入必要的参数uid' };
    }
    const update = {};
    if (username) {
      update.username = username;
    }
    if (uportrait) {
      update.uportrait = uportrait;
    }
    if (sex) {
      update.sex = sex;
    }
    if (birthday) {
      update.birthday = birthday;
    }
    if (role) {
      update.role = role;
    }
    if (area) {
      update.area = area;
    }
    if (phone) {
      update.phone = phone;
    }
    if (pas) {
      update.pas = utils.md5(pas);
    }
    if (signature) {
      update.signature = signature;
    }
    try {
      await ctx.model.User.update(
        update
        , {
          where: {
            uid,
          },
        });

      ctx.body = { success: true, msg: '修改成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '修改失败' };
    }
  }

  async userdestroy() {
    const { ctx } = this;
    const { uid } = ctx.request.body;
    try {
      await ctx.model.User.destroy({
        where: {
          uid,
        },
      });
      ctx.body = { success: true, msg: '删除成功' };
    } catch (e) {
      ctx.body = { success: true, msg: '删除失败' };
    }
  }

  async userfind() {
    const { ctx } = this;
    const { username, role, phone, uid, addTime, sex } = ctx.request.body;
    const where = {};
    if (username) {
      where.username = username;
    }
    if (role) {
      where.role = role;
    }
    if (phone) {
      where.phone = phone;
    }
    if (uid) {
      where.uid = uid;
    }
    if (addTime) {
      where.addTime = addTime;
    }
    if (sex) {
      where.sex = sex;
    }
    try {
      const res = await ctx.model.User.findAll(
        {
          where,
          attributes: {
            exclude: [ 'pas' ],
          },
        }
      );
      ctx.body = { success: true, data: res };
    } catch (e) {
      ctx.body = { success: true, msg: '查询失败' };
    }
  }

  async userlogin() {
    const { ctx } = this;
    const { phone, pas } = ctx.request.body;
    const password = utils.md5(pas);
    try {
      const res = await ctx.model.User.findOne({
        where: {
          phone,
        },
      });
      if (password === res.dataValues.pas) {
        ctx.body = { success: true, data: res };
      } else {
        ctx.body = { success: false, msg: '用户密码不匹配' };
      }

    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }

  async useradtime() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const { todaynews, todayends, yestodaynews, twonews, threenews, fournews, fivenews } = ctx.request.body;
    if (!(todaynews && todayends && yestodaynews && twonews && threenews && fournews && fivenews)) {
      return ctx.body = { success: false, msg: '请传入指定参数' };
    }
    try {
      const today = await ctx.model.User.count({
        where: {
          addTime: {
            [Op.between]: [ todaynews, todayends ],
          },
        },
      });
      const yestoday = await ctx.model.User.count({
        where: {
          addTime: {
            [Op.between]: [ yestodaynews, todaynews ],
          },
        },
      });
      const tow = await ctx.model.User.count({
        where: {
          addTime: {
            [Op.between]: [ twonews, yestodaynews ],
          },
        },
      });
      const three = await ctx.model.User.count({
        where: {
          addTime: {
            [Op.between]: [ threenews, twonews ],
          },
        },
      });
      const four = await ctx.model.User.count({
        where: {
          addTime: {
            [Op.between]: [ fournews, threenews ],
          },
        },
      });
      const five = await ctx.model.User.count({
        where: {
          addTime: {
            [Op.between]: [ fivenews, fournews ],
          },
        },
      });
      const usercount = [];
      usercount.push(five, four, three, tow, yestoday, today);
      ctx.body = { success: true, data: usercount };
    } catch (e) {
      ctx.body = { success: false, msg: '发生未知错误' };
    }
  }
}

module.exports = HomeController;
